package cn.iocoder.yudao.im.service.Impl;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.im.client.IMClient;
import cn.iocoder.yudao.im.common.model.IMPublicMessage;
import cn.iocoder.yudao.im.common.model.IMUserInfo;
import cn.iocoder.yudao.im.domain.NoticeImMessage;
import cn.iocoder.yudao.im.domain.NoticeImPublicMessage;
import cn.iocoder.yudao.im.enums.MessageStatus;
import cn.iocoder.yudao.im.mapper.NoticeImPublicMessageMapper;
import cn.iocoder.yudao.im.service.NoticeImMessageService;
import cn.iocoder.yudao.im.service.NoticeImPublicMessageService;
import cn.iocoder.yudao.im.util.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 群发消息记录表 服务实现类
 * </p>
 *
 * @author zcc
 * @since 2024-06-26
 */
@Service
public class NoticeImPublicMessageServiceImpl extends ServiceImpl<NoticeImPublicMessageMapper, NoticeImPublicMessage> implements NoticeImPublicMessageService {

// todo   Spring  注入失败
//    @Resource
//    SensitiveFilterUtil sensitiveFilterUtil;

    @Resource
    NoticeImMessageService messageService;

    @Resource
    IMClient imClient;


    @Override
    @Transactional
    public List<Map<String, Object>> sendMessage(NoticeImPublicMessage vo) {
        String recvId = vo.getRecvId();

        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        // 保存消息
        vo.setSendId(loginUser.getId());
        vo.setStatus(MessageStatus.UNSEND.code());
        vo.setSendTime(LocalDateTime.now());
        this.save(vo);
        //
        String[] split = recvId.split(",");
        List<String> list = Arrays.asList(split);
        List<Map<String, Object>> rList = new ArrayList<>();
        List<IMPublicMessage<NoticeImMessage>> sList = new ArrayList<>();
        for (String s : list) {
            NoticeImMessage noticeImMessage = new NoticeImMessage();
            BeanUtils.copyProperties(vo, noticeImMessage);
            noticeImMessage.setRecvId(Long.parseLong(s));
            noticeImMessage.setId(null);
            messageService.save(noticeImMessage);
            Map<String, Object> map = new HashMap<>();
            map.put("id", noticeImMessage.getId());
            map.put("userId", s);
            rList.add(map);
            IMPublicMessage<NoticeImMessage> sendMessage = new IMPublicMessage<>();
            sendMessage.setSender(new IMUserInfo(loginUser.getId(), 0));
            sendMessage.setRecvId(noticeImMessage.getRecvId());
            sendMessage.setSendToSelf(true);
            sendMessage.setData(noticeImMessage);
            sendMessage.setSendResult(true);
            sList.add(sendMessage);

        }


        // 过滤消息内容
//        String content = sensitiveFilterUtil.filter(vo.getContent());
//        vo.setContent(content);
        vo.setContent(vo.getContent());
        // 推送消息

        imClient.sendPublicMessage(sList);
        return rList;

    }
}
